<?php
/*
 * @Descripttion: 
 * @Author: 夜雨
 * @Email: 448408740@qq.com
 * @Date: 2019-12-24 14:51:51
 * @LastEditTime : 2019-12-25 17:12:43
 */
$url = "https://www.baidu.com/link?url=tidRzj41LZvsuaKMjOg5XZdbagHo_w9trwwTGI0qZHW&wd=&eqid=c944564b000882f0000000025e0326b0";
$eqid = substr($url, strpos($url, 'eqid=')+5, strlen($url));
echo $eqid;
 die;
$access_key = "3ada06d49eb34e67a0f1a6b025c52d7b";
$secret_key = "c94794c73ee5411c9e577393645ebc32";
$eqid = "ae7ea95900077d33000000025e02d954";
$url = "http://referer.bj.baidubce.com/v1/eqid/$eqid";
$method = "GET";
$utc_time_str = gmdate("Y-m-d\TH:i:s\Z");
$auth = gen_auth($access_key, $secret_key, $utc_time_str, $url, $method);
$header = [
    "accept-encoding: gzip, deflate",
    "host: referer.bj.baidubce.com",
    "content-type: application/json",
    "x-bce-date: $utc_time_str",
    "authorization: $auth",
    "accept:*/*"
];

echo curlRequest($url, $header);

function gen_auth($access_key, $secret_key, $utc_time_str, $url, $method){
    $url_parse_ret = parse_url($url);
    $host = $url_parse_ret["host"];
    $path = $url_parse_ret["path"];
    $version = "1";
    $expiration_seconds = "1800";
    $signature_headers = $host.";".$utc_time_str;
    $method = strtoupper($method);
    $val = "bce-auth-v$version/$access_key/$utc_time_str/$expiration_seconds";
    $signing_key = hash_hmac("sha256", $val, $secret_key);
    $canonical_uri = urlencode($path);
    $canonical_headers = "host:$host";
    $canonical_request = "$method\n$canonical_uri\n\n$canonical_headers";
    $signature = hash_hmac("sha256", $canonical_request, $signing_key, false);
    echo $signature;
    $authorization = "bce-auth-v$version/$access_key/$utc_time_str/$expiration_seconds/$signature_headers/$signature";
    return $authorization;
}

function curlRequest($url, $header, $data=null, $method='get', $https=false){
    $ch = curl_init();//初始化
    curl_setopt($ch, CURLOPT_URL, $url);//访问的URL
    curl_setopt($ch, CURLOPT_HEADER, false);//设置需要头信息
    //curl模拟头部信息
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只获取页面内容，但不输出
    if($https){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//https请求 不验证证书 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//https请求 不验证HOST
    }
    // curl_setopt($ch,CURLOPT_ENCODING,'gzip');//百度返回的内容进行了gzip压缩,需要用这个设置解析
    if($method == 'post'){
        curl_setopt($ch, CURLOPT_POST, true);//请求方式为post请求
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//请求数据
    }
    $result = curl_exec($ch);//执行请求
    curl_close($ch);//关闭curl，释放资源
    $result = mb_convert_encoding($result, 'utf-8', 'GBK,UTF-8,ASCII,gb2312');//百度默认编码是gb2312 这个设置转化为utf8编码
    return $result;
}